home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / as5.zip / AS.C next >
Text File  |  1987-12-09  |  4KB  |  218 lines

  1. char mapdn();
  2. char *alloc();
  3. /*
  4.  *    as ---    cross assembler main program
  5.  */
  6. main(argc,argv)
  7. int    argc;
  8. char    **argv;
  9. {
  10.     char    **np;
  11.     char    *i;
  12.     FILE    *fopen();
  13.     int    j = 0;
  14.  
  15.     if(argc < 2){
  16.         printf("Usage: %s [files]\n",argv[j]);
  17.         exit(1);
  18.         }
  19.       Argv = argv;
  20.       initialize();
  21.       while ((*argv[j] != '-') && (j<argc))
  22.        j++;
  23.       N_files = j-1;
  24.      if (j < argc )
  25.       {
  26.       argv[j]++;
  27.       while (j<argc)
  28.        {
  29.        for (i = argv[j]; *i != 0; i++)
  30.          if ((*i <= 'Z') && (*i >= 'A'))
  31.            *i = *i + 32;
  32.        if (strcmp(argv[j],"l")==0)
  33.          Lflag = 1;
  34.        else if (strcmp(argv[j],"nol")==0)
  35.          Lflag = 0;
  36.        else if (strcmp(argv[j],"c")==0)
  37.           Cflag = 1;
  38.        else if (strcmp(argv[j],"noc")==0)
  39.          Cflag = 0;
  40.        else if (strcmp(argv[j],"s")==0)
  41.          Sflag = 1;
  42.        else if (strcmp(argv[j],"cre")==0)
  43.          CREflag = 1;
  44.         j++;
  45.        }
  46.       }
  47.     root = NULL;
  48.  
  49.     Cfn = 0;
  50.     np = argv;
  51.     Line_num = 0; /* reset line number */
  52.     while( ++Cfn <= N_files )
  53.         if((Fd = fopen(*++np,"r")) == NULL)
  54.             printf("as: can't open %s\n",*np);
  55.         else{
  56.             make_pass();
  57.             fclose(Fd);
  58.         }
  59.     if( Err_count == 0 ){
  60.         Pass++;
  61.         re_init();
  62.         Cfn = 0;
  63.         np = argv;
  64.         Line_num = 0;
  65.         while( ++Cfn <= N_files)
  66.             if((Fd = fopen(*++np,"r")) != NULL)
  67.                 {
  68.                 make_pass();
  69.                 fclose(Fd);
  70.                  }
  71.             if (Sflag == 1)
  72.               {
  73.                 printf ("\f");
  74.                 stable (root);
  75.               }
  76.             if (CREflag == 1)
  77.               {
  78.                 printf ("\f");
  79.                 cross (root);
  80.               }
  81.         fprintf(Objfil,"S9030000FC\n"); /* at least give a decent ending */
  82.         }
  83.     exit(Err_count);
  84. }
  85.  
  86. initialize()
  87. {
  88.     FILE    *fopen();
  89.     int    i = 0;
  90.  
  91. #ifdef DEBUG
  92.     printf("Initializing\n");
  93. #endif
  94.     Err_count = 0;
  95.     Pc      = 0;
  96.     Pass      = 1;
  97.     Lflag      = 0;
  98.     Cflag      = 0;
  99.     Ctotal      = 0;
  100.     Sflag      = 0;
  101.     CREflag   = 0;
  102.     N_page      = 0;
  103.     Line[MAXBUF-1] = NEWLINE;
  104.  
  105.     strcpy(Obj_name,Argv[1]); /* copy first file name into array */
  106.     do {
  107.         if (Obj_name[i]=='.')
  108.            Obj_name[i]=0;
  109.     }
  110.     while (Obj_name[i++] != 0);
  111.     strcat(Obj_name,".s19");  /* append .out to file name. */
  112.     if( (Objfil = fopen(Obj_name,"w")) == NULL)
  113.         fatal("Can't create object file");
  114.     fwdinit();    /* forward ref init */
  115.     localinit();    /* target machine specific init. */
  116. }
  117.  
  118. re_init()
  119. {
  120. #ifdef DEBUG
  121.     printf("Reinitializing\n");
  122. #endif
  123.     Pc    = 0;
  124.     E_total = 0;
  125.     P_total = 0;
  126.     Ctotal    = 0;
  127.     N_page    = 0;
  128.     fwdreinit();
  129. }
  130.  
  131. make_pass()
  132. {
  133.     char    *fgets();
  134.  
  135. #ifdef DEBUG
  136.     printf("Pass %d\n",Pass);
  137. #endif
  138.     while( fgets(Line,MAXBUF-1,Fd) != (char *)NULL ){
  139.         Line_num++;
  140.         P_force = 0;    /* No force unless bytes emitted */
  141.         N_page = 0;
  142.            if(parse_line())
  143.             process();
  144.         if(Pass == 2 && Lflag && !N_page)
  145.             print_line();
  146.         P_total = 0;    /* reset byte count */
  147.         Cycles = 0;    /* and per instruction cycle count */
  148.         }
  149.     f_record();
  150. }
  151.  
  152.  
  153. /*
  154.  *    parse_line --- split input line into label, op and operand
  155.  */
  156. parse_line()
  157. {
  158.     register char *ptrfrm = Line;
  159.     register char *ptrto = Label;
  160.     char    *skip_white();
  161.  
  162.     if( *ptrfrm == '*' || *ptrfrm == '\n' )
  163.         return(0);    /* a comment line */
  164.  
  165.     while( delim(*ptrfrm)== NO )
  166.         *ptrto++ = *ptrfrm++;
  167.     if(*--ptrto != ':')ptrto++;     /* allow trailing : */
  168.     *ptrto = EOS;
  169.  
  170.     ptrfrm = skip_white(ptrfrm);
  171.  
  172.     ptrto = Op;
  173.     while( delim(*ptrfrm) == NO)
  174.         *ptrto++ = mapdn(*ptrfrm++);
  175.     *ptrto = EOS;
  176.  
  177.     ptrfrm = skip_white(ptrfrm);
  178.  
  179.     ptrto = Operand;
  180.     while( *ptrfrm != NEWLINE )
  181.         *ptrto++ = *ptrfrm++;
  182.     *ptrto = EOS;
  183.  
  184. #ifdef DEBUG
  185.     printf("Label-%s-\n",Label);
  186.     printf("Op----%s-\n",Op);
  187.     printf("Operand-%s-\n",Operand);
  188. #endif
  189.     return(1);
  190. }
  191.  
  192. /*
  193.  *    process --- determine mnemonic class and act on it
  194.  */
  195. process()
  196. {
  197.     register struct oper *i;
  198.     struct oper *mne_look();
  199.  
  200.     Old_pc = Pc;        /* setup `old' program counter */
  201.     Optr = Operand;     /* point to beginning of operand field */
  202.  
  203.     if(*Op==EOS){        /* no mnemonic */
  204.         if(*Label != EOS)
  205.             install(Label,Pc);
  206.         }
  207.     else if( (i = mne_look(Op))== NULL)
  208.         error("Unrecognized Mnemonic");
  209.     else if( i->class == PSEUDO )
  210.         do_pseudo(i->opcode);
  211.     else{
  212.         if( *Label )install(Label,Pc);
  213.         if(Cflag)Cycles = i->cycles;
  214.         do_op(i->opcode,i->class);
  215.         if(Cflag)Ctotal += Cycles;
  216.         }
  217. }
  218.